package cn.xyy.system.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.xyy.system.dao.JobDao;
import cn.xyy.web.AuthUserHolder;
import cn.xyy.common.domain.system.auth.Job;
import cn.xyy.common.domain.system.auth.JobVO;
import cn.xyy.common.utils.BeanUtils;
import cn.xyy.common.utils.DateUtils;
import cn.xyy.common.utils.StringUtils;
import cn.xyy.common.exception.SystemException;
import cn.xyy.common.domain.mvc.Page;

/**
 * 岗位业务层
 * 
 * @author xyy
 *
 */
@Service
public class JobService {
	@Autowired
	private JobDao jobDao;

	public Page<Job> getJobPage(JobVO vo) {
		if (vo == null) {
			throw new SystemException("参数不能为空");
		}
		return jobDao.selectPage(vo);
	}

	public List<Job> getJobList() {
		return jobDao.selectAll();
	}

	public void add(Job job) {
		if (StringUtils.isBlank(job.getName())) {
			throw new SystemException("岗位名不能为空");
		}
		if (job.getStatus() == null) {
			throw new SystemException("状态不能为空");
		}
		job.setCreator(AuthUserHolder.getUserInfo().getUsername());
		job.setCreateTime(DateUtils.getDateTime());
		jobDao.insert(job);
	}

	public void update(JobVO vo) {
		if (vo.getId() == null) {
			throw new SystemException("id不能为空");
		}
		if (StringUtils.isBlank(vo.getName())) {
			throw new SystemException("岗位名不能为空");
		}
		if (vo.getStatus() == null) {
			throw new SystemException("状态不能为空");
		}
		Job job = jobDao.selectOne(new Job().setId(vo.getId()));
		if (job == null) {
			throw new SystemException("未查询到信息");
		}
		BeanUtils.copyProperties(vo, job);
		job.setUpdater(AuthUserHolder.getUserInfo().getUsername());
		job.setUpdateTime(DateUtils.getDateTime());

		jobDao.update(job);

	}

	public Job getJob(String id) {
		return jobDao.selectOne(new Job().setId(id));
	}

	public void delete(String id) {
		if (id == null) {
			throw new SystemException("id不能为空");
		}
		jobDao.delete(new Job().setId(id));
	}

	public void updateState(Job job) {
		if (job.getId() == null) {
			throw new SystemException("id不能为空");
		}
		jobDao.updateState(job.getId(), job.getStatus());
	}
}
